package com.ch.sys.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ch.sys.common.Constants;
import com.ch.sys.model.Role;
import com.ch.sys.service.RoleService;
import com.ch.sys.utils.ParamMap;
import com.ch.sys.utils.Response;

@Controller
@RequestMapping(Constants.REST_WEB_URL)
public class RoleController extends BaseController {
	
	@Autowired
	RoleService roleService;
	
	/**
	 * 分页 查询
	 */
	@ResponseBody
	@RequestMapping(value="/role/{page}/{limit}", method = RequestMethod.GET)
	public Response findPage(@PathVariable Integer page, @PathVariable Integer limit) {
		ParamMap param = ParamMap.newMap();
		param.put("pageIndex", page);
		param.put("pageSize", limit);
		
		return roleService.findPage(param);
	}
	
	@ResponseBody
	@RequestMapping(value="/role", method = RequestMethod.GET)
	public Response findByPage(HttpServletRequest request) {
		ParamMap param = ParamMap.init(request);
		return roleService.findPage(param);
	}

	/**
	 * 添加
	 * 如果需要@RequestBody Role role能接收到数据，
	 * 客户端请使用application/json的方式提交数据，非表单提交
	 */
	@ResponseBody
	@RequestMapping(value = "/role", method = RequestMethod.POST)
	public Response add(@RequestBody Role role) {
		Response response = roleService.add(role);
		return response.ok(role);
	}
	
	/**
	 * 修改
	 * 如果需要@RequestBody Role role能接收到数据，
	 * 客户端请使用application/json的方式提交数据，非表单提交
	 */
	@ResponseBody
	@RequestMapping(value = "/role", method = RequestMethod.PUT)
	public Response update(@RequestBody Role role) {
		Response response = roleService.update(role);
		return response.ok(role);
	}
}
